package com.auto.code.controller;

import com.auto.code.dao.TemplateGroupDAO;
import com.auto.code.domain.DBInfoDTO;
import com.auto.code.domain.ResultInfo;
import com.auto.code.domain.TemplateGroupDTO;
import com.auto.code.service.DBInfoService;
import com.auto.code.service.TemplateGroupService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 模板分类控制层
 */
@Controller
@RequestMapping("/auto/code/group")
public class TemplateGroupController {
	@Autowired
    private TemplateGroupService templateGroupService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest request) {
        return new ModelAndView("/page/group/group_list");
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ModelAndView tableList(Model model, TemplateGroupDTO templateGroupDTO) {
        PageInfo<TemplateGroupDTO> pageList = templateGroupService.queryPage(templateGroupDTO);
        model.addAttribute("pageInfo", pageList);
        return new ModelAndView("/page/group/group_table_list");
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView createPage(Model model, TemplateGroupDTO templateGroupDTO) {
        return new ModelAndView("/page/group/group_create");
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo createSave(Model model, TemplateGroupDTO templateGroupDTO) {
        templateGroupService.create(templateGroupDTO);
        return  ResultInfo.ok();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo del(@RequestParam(required = false, value = "idList[]") List<Long> idList) {
	    for (Long id : idList){
            templateGroupService.deleteById(id);
        }
        return ResultInfo.ok();
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public ModelAndView modifyPage(Model model, Long id) {
        model.addAttribute("group", templateGroupService.getById(id));
        return new ModelAndView("/page/group/group_modify");
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo modifySave(Model model, TemplateGroupDTO templateGroupDTO) {
        templateGroupService.update(templateGroupDTO);
        return  ResultInfo.ok();
    }
}
